import 'package:flutter/material.dart';
import 'package:graphql_flutter/graphql_flutter.dart';

import '../data/order.dart';
import '../api/graphql/order_gql.dart';

class OrderModel extends ChangeNotifier {
  List<Order>? _orderList = [];

  bool _isLoading = false;
  bool _isLoadOrdersCalled = false;
  String? _errorMessage;

  List<Order> get orderList => _orderList ?? [];
  
  // 订单状态相关方法
  String getStatusText(String? status) {
    // 如果状态为空或null，则显示全部
    if (status == null || status.isEmpty) {
      return '全部';
    }
    
    switch (status) {
      case 'non_pay':
        return '未支付';
      case 'undelivered':
        return '未配送';
      case 'unconfirmed':
        return '未确认';
      case 'canceled':
        return '已取消';
      case 'completed':
        return '已完成';
      default:
        return status ?? '未知状态';
    }
  }

  Color getStatusColor(String? status) {
    // 如果状态为空或null，则使用默认颜色
    if (status == null || status.isEmpty) {
      return Colors.blue;
    }
    
    switch (status) {
      case 'non_pay':
        return Colors.orange;
      case 'undelivered':
        return Colors.blue;
      case 'unconfirmed':
        return Colors.grey;
      case 'canceled':
        return Colors.red;
      default:
        return Colors.black;
    }
  }

  String getStatusTitle(String? status) {
    // 如果状态为空或null，则显示全部订单
    if (status == null || status.isEmpty) {
      return '全部订单';
    }
    
    switch (status) {
      case 'non_pay':
        return '未支付订单';
      case 'undelivered':
        return '未配送订单';
      case 'unconfirmed':
        return '未确认订单';
      case 'canceled':
        return '已取消订单';
      default:
        return '全部订单';
    }
  }
  
  bool get isLoading => _isLoading;
  bool get isLoadOrdersCalled => _isLoadOrdersCalled;
  String? get errorMessage => _errorMessage;



  // 映射状态到查询参数
  String mapStatusToQuery(String status) {
    switch (status) {
      case '未支付':
      case 'non_pay':
        return 'non_pay';
      case '未配送':
      case 'undelivered':
        return 'undelivered';
      case '未确认':
      case 'unconfirmed':
        return 'unconfirmed';
      case '已取消':
      case 'canceled':
      case 'cancel':
        return 'canceled';
      default:
        return '';
    }
  }

  // 加载订单列表
  Future<void> loadOrders(GraphQLClient client, String? status) async {
    _isLoadOrdersCalled = true;
    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

    try {
      final result = await client.query(
        QueryOptions(
          document: gql(OrderGQL.fetchOrderList),
          variables: {
            'status': status,
            'limit': 10,
            'cursor': 0,
          },
          fetchPolicy: FetchPolicy.networkOnly,
        ),
      );

      if (result.hasException) {
        _errorMessage = '加载订单失败: ${result.exception.toString()}';
      }

      final ordersData = result.data!['order']['myOrders']['orders'] as List;
      _orderList = ordersData.map((item) => Order.fromJson(item)).toList();

    } catch (e) {
      _errorMessage = '加载订单异常: $e';
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }
}